package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.INavigationDao;
import com.cn.wanxi.dao.impl.NavigationDaoImpl;
import com.cn.wanxi.model.NavigationModel;
import com.cn.wanxi.service.INavigationService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavigationServiceImpl implements INavigationService {

//    //线程
//    @Override
//    public List<NavigationModel> findAll() {
//        final List<NavigationModel>[] list = new List[]{new ArrayList<>()};
//        new Thread(new Runnable(){
//            @Override
//            public void run() {
//                list[0] =getListForThread();
//            }
//        }).start();
//        return list[0];
//    }
//    private synchronized List<NavigationModel> getListForThread(){
//        INavigationDao iNavigationDao=new NavigationDaoImpl();
//        Jedis jedis=new Jedis();
//        List<NavigationModel> list=new ArrayList<>();
//        Long length=jedis.llen("ids");
//        if (length > 0){//判断redis的id是否有数据
////            拿到redis数据库全部数据
//            List<String> ids=jedis.lrange("ids",0,-1);
//            List<String> names=jedis.lrange("names",0,-1);
//            List<String> connections=jedis.lrange("connections",0,-1);
//            for (int i=0; i<length;i++){//封装到model
//                NavigationModel navigationModel=new NavigationModel();
//                navigationModel.setId(Integer.parseInt(ids.get(i)));
//                navigationModel.setName(names.get(i));
//                navigationModel.setConnection(connections.get(i));
//                list.add(navigationModel);
//            }
//        }else {
//            list=iNavigationDao.findAll();
//            for (NavigationModel model:list) {
//                jedis.rpush("ids",model.getId().toString());
//                jedis.rpush("names",model.getName());
//                jedis.rpush("connections",model.getConnection());
//            }
//        }
//        return list;
//    }


    @Override
    public List<NavigationModel> findAll() {
        INavigationDao iNavigationDao=new NavigationDaoImpl();
        Jedis jedis=new Jedis();
        List<NavigationModel> list=new ArrayList<>();
        Long length=jedis.llen("ids");
        if (length > 0){//判断redis的id是否有数据
//            拿到redis数据库全部数据
            List<String> ids=jedis.lrange("ids",0,-1);
            List<String> names=jedis.lrange("names",0,-1);
            List<String> connections=jedis.lrange("connections",0,-1);
            List<String> isshow=jedis.lrange("isShow",0,-1);
            for (int i=0; i<length;i++){//封装到model
                NavigationModel navigationModel=new NavigationModel();
                navigationModel.setId(Integer.parseInt(ids.get(i)));
                navigationModel.setName(names.get(i));
                navigationModel.setConnection(connections.get(i));
                navigationModel.setIsShow(isshow.get(i));
                list.add(navigationModel);
            }
        }else {
            list=iNavigationDao.findAll();
            for (NavigationModel model:list) {
                jedis.rpush("ids",model.getId().toString());
                jedis.rpush("names",model.getName());
                jedis.rpush("connections",model.getConnection());
                jedis.rpush("isShow",model.getIsShow());
            }
        }
        return list;
    }





    @Override
    public int deleteById(int navigationid) {
        INavigationDao iNavigationDao=new NavigationDaoImpl();
        int count=iNavigationDao.deleteById(navigationid);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("ids");//删除redis缓存id
            jedis.del("names");
            jedis.del("connections");
            jedis.del("isShow");

        }
        return count;
    }

    @Override
    public NavigationModel findById(int navigationid) {
        INavigationDao iNavigationDao=new NavigationDaoImpl();
        return iNavigationDao.findById(navigationid);
    }

    @Override
    public int update(NavigationModel navigationModel) {
        INavigationDao iNavigationDao=new NavigationDaoImpl();
        int count=iNavigationDao.update(navigationModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("ids");//删除redis缓存id
            jedis.del("names");
            jedis.del("connections");
            jedis.del("isShow");
        }
        return count;
    }

    @Override
    public int navigationAdd(NavigationModel navigationModel) {
        INavigationDao iNavigationDao=new NavigationDaoImpl();
        int count=iNavigationDao.navigationAdd(navigationModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("ids");//删除redis缓存id
            jedis.del("names");
            jedis.del("connections");
            jedis.del("isShow");
        }
        return count;
    }

}
